var login = function() {
    window.location.href = 'index.html';
};
var token= localStorage.getItem('login-token');
var username= localStorage.getItem('username');

var role;

(function() {
    $.ajax({
        url:'/admin/user-account/get-role',
        type:"POST",
        beforeSend: function(request) {
            request.setRequestHeader("OPERATOR-ID", username);
            request.setRequestHeader("LOGIN-TOKEN", token);
        },
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(response) {
            if(response.code === 304) {
                login();
            }else if(response.code === 200) {
                role = response.data;
                localStorage.setItem('user-role', role);
                if(role !== 'ADMIN') {
                    $('#client-manage').hide();
                    $('#user-manage').hide();
                    $('.admin').hide();
                }
            }else {
                alert(response.message);
            }
        }
    });
    $('#username-display').text('用户：' + username);
})();

var getAllSystemId = function(callback) {
    $.ajax({
        url:'/admin/client-info/list',
        type:"POST",
        beforeSend: function(request) {
            request.setRequestHeader("OPERATOR-ID", username);
            request.setRequestHeader("LOGIN-TOKEN", token);
        },
        data: JSON.stringify({}),
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(response) {
            if(response.code === 304) {
                login();
            }else if(response.code === 200) {
                callback(response.data);
            }else {
                alert(response.message);
            }
        }
    });
};

(function() {
    $('#logout').bind('click', function() {
        localStorage.removeItem('login-token');
        localStorage.removeItem('username');
        login();
    });
})();